﻿@model IndexViewModel

@{
    ViewBag.Title = "全站搜索";
    ViewBag.PageTitle = ViewBag.Title + "_内容";
}

<div class="content_left">
  <span style="color: green;">@TempData["Result"]</span>
  <span style="color: red;">@TempData["ResultFail"]</span>
</div>

@section header{
<link href="~/bower_components/ekko-lightbox/dist/ekko-lightbox.min.css" rel="stylesheet" />
    <link href="~/plugins/SigFinder/SigFinder.min.css" rel="stylesheet" />
    <link href="~/plugins/webuploader/webuploader.css" rel="stylesheet" />
}

<div class="panel panel-default">
    <div class="panel-heading">
        <div class="row">
            <div class="col-sm-8">
                @using (Html.BeginForm("Index", "Lucene", FormMethod.Get, new { @class = "form-inline" }))
                {
                  
                        <div class="form-group">                          
                            @Html.TextBoxFor(m => m.SearchTerm, new { @class = "form-control", autocomplete = "off", placeholder= "关键字" })
                        </div>
                        <div class="form-group">
                            @Html.DropDownListFor(m => m.SearchField, Model.SearchFieldList.Select(x => new SelectListItem { Text = x.Text, Value = x.Value }),
          new { @class = "form-control" })
                        </div>
                        <div class="form-group">
                            @Html.CheckBoxFor(m => m.SearchDefault)
                            @Html.LabelFor(m => m.SearchDefault, "使用默认Lucene查询")
                        </div>
                        <button type="submit" class="btn btn-primary"><i class="fa fa-search fa-fw"></i>查找</button>

                        }
                    </div>
            <div class="col-sm-4 text-right">
                @Ajax.RawActionLink("<i class='fa fa-plus fa-fw'></i>添加索引",
                        "AddToIndex", "Lucene",
                        new { },
                        new AjaxOptions { HttpMethod = "Get", UpdateTargetId = "edit-container", OnComplete = "onAjaxComplete", OnSuccess = "OnSuccessLink" },
                        new { @class = "btn btn-primary" })
                <a href="#" data-url="@Url.Action("OptimizeIndex")" class="btn btn-primary optimize"><i class="fa fa-search fa-fw"></i>搜索优化</a>
                <a href="#" data-url="@Url.Action("ClearIndex")" class="btn btn-danger clearIndex"><i class="fa fa-trash-o fa-fw"></i>清空索引</a>
               
            </div>
        </div>
        
       
    </div>


    
    @if (Model.SearchIndexData.Any()) {
      <table class="table table-striped">
        <tr>
          <th>图片</th>
          <th>标题</th>
          <th>网址</th>
            <th>创建日期</th>
          <th>操作</th>
        </tr>
        @foreach (var record in Model.SearchIndexData)
        {
            <tr class="item-container">
                <td>
                    @if (!string.IsNullOrEmpty(record.ImageUrl))
                    {
                       
                        <a href="@string.Format("{0}?w=800&mode=w", record.ImageUrl)" data-toggle="lightbox" data-gallery="multiimages" data-title="@record.Name">
                            <img src="@string.Format("{0}?w=60&h=30&mode=crop", record.ImageUrl)" alt="" />
                        </a>
                    }
                </td>
                <td>@record.Name</td>
                <td>@record.Url</td>
                <td>@record.CreatedDate</td>
                <td>
                    <a href="#" data-url="@Url.Action("ClearIndexRecord", new {id = record.Id})" class="btn btn-danger btn-sm delete-item" title="@Buttons.Delete">
                        <i class="fa fa-trash-o fa-fw"></i> @Buttons.Delete
                    </a>
                </td>
            </tr>
        }		
      </table>
    }
    else
    {
        <div class="panel-body">
            <p>没有找到任何索引记录！</p>
        </div>
    }
    <footer class="panel-footer">
        <div class="row">
            <div class="col-sm-7">
                @Html.PagedListPager(Model.SearchIndexData, page => Url.Action("Index", new { page = page }))
            </div>
            <div class="col-sm-5">
                <div class="form-inline text-right">
                    <div class="input-group">
                        <span class="input-group-addon" id="basic-addon3"> 总记录<strong>@Model.TotalCount</strong>条，分页</span>
                        @Html.DropDownListFor(m => Model.PageSize, (SelectList)ViewBag.PageSizes, "分页", new { @class = "form-control" })
                    </div>
                </div>
            </div>
        </div>


    </footer>

</div>

@Html.Action("PopuFinder", "File")

<div id="edit-container">
</div>

<form id="anti-form" method="post">
    @Html.AntiForgeryToken()
</form>

@section footer {
    @Scripts.Render("~/bundles/jqueryvalWithBootstrap")

<script src="~/bower_components/ekko-lightbox/dist/ekko-lightbox.min.js"></script>
<script src="/Plugins/webuploader/webuploader.min.js"></script>
<script src="/Plugins/SigFinder/FileManager.js"></script>

<script type="text/javascript">

    function SetImageUrl() {
        var fileUrl = $("#fileList .item.active").attr("data-file");
        $('#ImageUrl').val(fileUrl);
        $('#fileManagerModal').modal('hide');
    }

        function OnSuccessLink(data) {
            Common.ShowBox(data.Status, data.Message, "操作")
        }

        function onAjaxComplete() {
            $('#ModalEdit').modal();

            $(document).on("click", "#btnImageUrl", function () {
                $('#fileManagerModal').modal();
                $("#selectImage").unbind('click');
                $("#selectImage").bind("click", function () {
                    SetImageUrl();
                });
            });
        }
        function onBegin() {
            $("#btnSave i").removeClass("fa-save").addClass("fa-spinner fa-spin")
        }
        function onComplete() {
            $("#btnSave i").removeClass("fa-spinner fa-spin").addClass("fa-save")
        }


        function updateSuccess(data) {
            var title = '@ViewBag.PageTitle';
            Common.ShowBoxWithFuncBack(data, title, submitSuccess);
        }

        function submitSuccess(id,htmlData) {
            $('#ModalEdit').modal('hide');
            window.location.reload(true);
        }


        $(document).ready(function () {


            //lightbox events
            $(document).delegate('*[data-toggle="lightbox"]', 'click', function (event) {
                event.preventDefault();
                $(this).ekkoLightbox();
            });
	
            $(document).on("change", '#PageSize', function (e) {
                e.preventDefault();

                var url = "@Url.Action("PageSizeSet")", title = "分页设置", pageSize = $(this).val();
                Common.PageSizeSet(url, title, pageSize, setPageSizeFunc);

            });

            function setPageSizeFunc() {
                location.reload();
            }

         //delete
            $(document).on("click", '.delete-item', function (e) {
                e.preventDefault();

                var url = $(this).attr('data-url');
                var that = $(this);
                bootbox.setLocale("zh_CN");
                bootbox.confirm("你确定要删除此项吗？", function (result) {
                    /*DELETE*/
                    if (result) {
                        var title = "@ViewBag.Title";
                        Common.DeleteItem(url, title, that);
                    }

                });

        });


        // create index
        $(document).on("click", '.optimize', function (e) {
            e.preventDefault();
            var url = $(this).attr('data-url');
            var that = $(this);
            bootbox.setLocale("zh_CN");
            bootbox.confirm("你确定要优化索引目录吗？", function (result) {
                if (result) {
                    var title = "优化索引目录";
                    $.post(url, $("#anti-form").serialize(), function (data) {
                        Common.ShowBox(data.Status, data.Message, title);
                    })
                }
            });
        });

        $(document).on("click", '.clearIndex', function (e) {
            e.preventDefault();
            var url = $(this).attr('data-url');
            var that = $(this);
            bootbox.setLocale("zh_CN");
            bootbox.confirm("你确定要清空索引目录吗？", function (result) {
                if (result) {
                    var title = "清空索引目录";
                    $.post(url, $("#anti-form").serialize(), function (data) {
                        Common.ShowBoxWithFunc(data, title, clearIndex);
                    })
                }
            });
        });

        });
        function clearIndex() {
            $(".table").remove();
        }
</script>

}